package com.mic.gae.resource.exception;

import java.net.URI;
import java.net.URISyntaxException;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class WebserviceExceptionMapper implements ExceptionMapper<Exception> {

	@Override
	public Response toResponse(Exception exception) {

		System.out.println("Exceptionhandling start...");

		if (exception instanceof UserNotLoggedInException) {

			try {
				return Response.seeOther(new URI(((UserNotLoggedInException) exception).getUrlToContinue())).build();
			} catch (URISyntaxException e) {

			}
		}

		// None of our exceptions
		return Response.status(Status.INTERNAL_SERVER_ERROR)
		        .entity(exception.getMessage() + ". cause: " + exception.getMessage()).type(MediaType.TEXT_PLAIN)
		        .build();

	}

}